<?php
namespace common\helpers;

use Yii;
class TFileHelper{

	public static function buildPath($pathes, $withStart = false, $withEnd = false)
	{
		$ret = '';

		foreach ($pathes as $path)
		{
			$ret .= $path . DIRECTORY_SEPARATOR;
		}
		if ($withStart)
		{
			$ret = DIRECTORY_SEPARATOR . $ret;
		}
		if (!$withEnd)
		{
			$ret = rtrim($ret, DIRECTORY_SEPARATOR);
		}
		return $ret;
	}

	public static function isDir($path)
	{
		return is_dir($path);
	}
	public static function exist($path)
	{
		if (is_array($path))
		{
			$path = self::buildPath($path);
		}
		return file_exists($path);
	}
	public static function getFiles($path, $prefix = null)
	{
		if (is_array($path))
		{
			$path = self::buildPath($path);
		}

		if(!is_dir($path))
		{
			//var_dump($path);
			return [];
		}

		$files = scandir($path);
		if ($prefix == null)
		{
			return $files;
		}

		$ret = [];
		foreach ($files as $file)
		{
			if (strpos($file, $prefix) === 0)
			{
				$ret[] = $file;
			}
		}

		return $ret;
	}

	public static function createFile($filePath, $content)
	{}

	public static function removeFile($filePath)
	{}

	public static function readFile($filePath)
	{
		if (is_array($filePath))
		{
			$filePath = self::buildPath($filePath);
		}

		return file_get_contents($filePath);
	}

	public static function writeFile($filePath, $content, $mode = 'w')
	{
		if (is_array($filePath))
		{
			$filePath = self::buildPath($filePath);
		}

		$f = fopen($filePath, $mode);
		fwrite($f, $content);
		fclose($f);
	}

	public static function createDir($dirPath)
	{}

    /**
     * 遍历删除文件目录
     * @param $dirName
     * @param bool $andDir
     */
	public static function removeDir($dirName,$andDir = true)
	{
        if ( $handle = opendir( "$dirName" ) ) {

            while ( false !== ( $item = readdir( $handle ) ) ) {

                if ( $item != "." && $item != ".." ) {

                    if ( is_dir( "$dirName/$item" ) ) {

                        self::removeDir( "$dirName/$item" );

                    } else {
                        if ($item != '.gitignore') {
                            unlink("$dirName/$item");
                        }
                    }
                }
            }

            closedir( $handle );
            if($andDir){
                rmdir( $dirName );
            }
        }
    }

    /**
     * 检查目录是否存在，如果不存在，则创建
     * @param $dir
     * @param bool $create
     * @param int $mode
     * @return bool
     */

    public static function check_exist_dir($dir, $create = true, $mode = 0755)
    {
        if (is_dir($dir) || @mkdir($dir,$mode)){
            return true;
        }
        if (!self::check_exist_dir(dirname($dir),$mode)){
            return false;
        }
        return @mkdir($dir,$mode);
    }

    /**
     * 解压zip文件包，默认指定到当前目录
     * @param $zipPath
     * @param null $dirPath
     * @return null|string
     */
    public static function unzip($zipPath,$dirPath = null){
        $zip = new \ZipArchive();

        $zipFullPath = $zipPath;
        $fileInfo = pathinfo($zipFullPath);

        if($dirPath == null){
            $dirPath .= $fileInfo['filename'];
        }

        $res = $zip->open($zipFullPath);
        if ($res === TRUE) {
            self::check_exist_dir($dirPath);
            $zip->extractTo($dirPath);
            $zip->close();

            return $dirPath;
        } else {
            return 'failed, code:' . $res;
        }
    }

}